//
//  NSMutableAttributedString+Extension.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/24.
//  Copyright © 2018 Mr.wang. All rights reserved.
//

import UIKit

extension NSMutableAttributedString {

    func setParagraphSpace(_ lineSpace : CGFloat, alignment: NSTextAlignment = .left) {
        let paragraphStye = NSMutableParagraphStyle()
        paragraphStye.alignment = alignment
        paragraphStye.lineSpacing =  lineSpace
        let range = NSMakeRange(0, self.length)
        self.addAttribute(.paragraphStyle, value: paragraphStye, range: range)
    }
    
    func setTextColor(_ color: UIColor) {
        setTextColor(color, in: NSMakeRange(0, self.length))
    }
    
    func setTextColor(_ color: UIColor, text: String) {
        if let range = string.range(of: text) {
            setTextColor(color, in: string.toNSRange(range))
        }
    }
    
    func setTextFont(_ font: UIKit.UIFont, text: String) {
        if let range = string.range(of: text) {
            self.addAttributes([.font: font], range: string.toNSRange(range))
        }
    }
    
    func setUnderLine(_ color: UIColor, text: String) {
        if let range = string.range(of: text) {
            self.addAttributes([.underlineColor: color,
                                .underlineStyle: NSNumber.init(value: 1)], range: string.toNSRange(range))
        }
    }
    
    func setTextColor(_ color: UIColor, in range: NSRange) {
        self.addAttributes([.foregroundColor: color], range: range)
    }
    
    func setTextBackgroundColor(_ color: UIColor, in range: NSRange) {
        self.addAttributes([.backgroundColor: color], range: range)
    }
}
